/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.sqlverboser;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.swing.*;
import jdbcmanager.dbtools.*;

/**
 * An SQL berboser to show SQL commands, as well as saving and retrieving sessions.
 * <p>
 * This class is in charge of:
 * <ul>
 * <li>Showing all executed the SQL commands and the time they took to be executed (the time starts
 * when the command is sent to the DMBS and ends when the control is returned back to the application).
 * <li>Saving to file and retrieving from file all currently open internal frames (only currently open: 
 * it is too much complicate (almost impossible) to do this properly for an entire session). 
 * </ul>
 *
 * @author Francisco Morero Peyrona
 */
/* NEXT: The way to show the help is not the appropriate one: see MouseListener */
public final class JSqlVerboser extends JPanel
{
   private TablePane        table   = new TablePane();
   private JToolBar         toolbar = new JToolBar();
   private TheMouseListener tml     = new TheMouseListener();
   
   // Actions
   private ShowAction       showAction = new ShowAction();
   private SaveAction       saveAction = new SaveAction();
   private LoadAction       loadAction = new LoadAction();
   
   //-------------------------------------------------------------------------//
   
   /**
    * Class constructor
    */
   public JSqlVerboser()
   {
      init();
   }

   /**
    * 
    * @param sCatalog
    * @param sSQL
    * @param nTime
    */
   public void add( String sCatalog, String sSQL, long nTime )
   {
      this.table.add( sCatalog, sSQL, nTime );
      updateActions();
   }

   /**
    * 
    */
   // Can't name this method "removeAll()" because exists in JPanel
   public void clear()
   {
      this.table.clearTable();
      updateActions();
   }
   
   /**
    * Changes first column header title to reflect preferred DBMS term for Catalog.
    * <p>
    * This is just a cosmetic method.
    */
   public void updateCatalogTerm()
   {
      this.table.updateCatalogTerm();
   }

   //-------------------------------------------------------------------------//
   
   private void showSelectedSQL()
   {
      this.table.inspectSelectedRow();
      updateActions();
   }
   
   private void saveSession()
   {
      this.loadAction.setEnabled( false );
      this.saveAction.setEnabled( false );
      App.sessionSave();
      updateActions();
   }
   
   private void loadSession()
   {
      App.sessionLoad( null );
      updateActions();
   }
   
   private void updateActions()
   {
      this.saveAction.setEnabled( DbUtil.getAgentDBMS() != null );  // At least there is a connection stablished
      this.showAction.setEnabled( this.table.getRowCount() > 0 );
      this.loadAction.setEnabled( App.sessionAreThereSaved() );
   }
   
   private void init()
   {
      this.toolbar.setOrientation( SwingConstants.VERTICAL );
      this.toolbar.setFloatable( true );
      this.toolbar.add( this.showAction );
      this.toolbar.add( this.saveAction );
      this.toolbar.add( this.loadAction );
      
      updateActions();
      
      setLayout( new BorderLayout() );

      this.table.addMouseListener( tml );
      this.toolbar.addMouseListener( tml );

      add( this.table   , BorderLayout.CENTER );
      add( this.toolbar , BorderLayout.WEST   );
   }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS
   //-------------------------------------------------------------------------//      
   private final class ShowAction extends JAbstractAction
   {
      private ShowAction()
      {
         setName( "Show" );
         setActionCommandKey( "SQL_CMD_SHOW" );
         setShortDescription( "Show" );
         setLongDescription( "Shows highlighted SQL command [Also Double-Click on table row]" );
         setSmallIcon( new ImageIcon( getClass().getResource( "images/zoom.gif" ) ) );
      }
      
      public void actionPerformed( ActionEvent evt )
      {
         if( isEnabled() )
            showSelectedSQL();
      }
   }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS
   //-------------------------------------------------------------------------//   
    private final class LoadAction extends JAbstractAction
    {
       private LoadAction()
       {
          setName( "Load" );
          setActionCommandKey( "SESSION_LOAD" );
          setShortDescription( "Load" );
          setLongDescription( "Retrieves from file a previously saved session" );
          setSmallIcon( new ImageIcon( getClass().getResource( "images/load.gif" ) ) );
       }
      
       public void actionPerformed( ActionEvent evt )
       {
          if( isEnabled() )
             loadSession();
       }      
    }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS
   //-------------------------------------------------------------------------//   
    private final class SaveAction extends JAbstractAction
    {
       private SaveAction()
       {
          setName( "Save" );
          setActionCommandKey( "SESSION_SAVE" );
          setShortDescription( "Save" );
          setLongDescription( "Saves session (currently opened windows) to a file" );
          setSmallIcon( new ImageIcon( getClass().getResource( "images/save.gif" ) ) );
       }

       public void actionPerformed( ActionEvent evt )
       {
          if( isEnabled() )
             saveSession();
       }      
    }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS: to update the Help context
   //-------------------------------------------------------------------------//

   private final class TheMouseListener extends MouseAdapter
   {
      public void mouseClicked( MouseEvent evt )
      {
         App.getFrameMain().setHelpContext( jdbcmanager.swing.help.JHelp.VERBOSER );
      }
   }
}
